package ee.android.kinokavad.data.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class SQLiteHelper extends SQLiteOpenHelper{

	public static final String TABLE_CINEMAS = "cinemas";
	public static final String COLUMN_CINEMA_ID = "_id";
	public static final String COLUMN_CINEMA_NETID = "net_id";
	public static final String COLUMN_CINEMA_NAME = "name";
	public static final String COLUMN_CINEMA_CITY = "city";
	public static final String COLUMN_CINEMA_SELECTED = "selected";

	public static final String COLUMN_CINEMA_PHONE = "phone";
	public static final String COLUMN_CINEMA_EMAIL = "email";
	public static final String COLUMN_CINEMA_ADDRESS = "address";
	public static final String COLUMN_CINEMA_LAT = "lat";
	public static final String COLUMN_CINEMA_LON = "lon";
	public static final String COLUMN_CINEMA_INFO = "info";
	public static final String COLUMN_CINEMA_HOMEPAGE = "homepage";

	public static final String TABLE_MOVIES = "movies";
	public static final String COLUMN_MOVIE_ID = "_id";
	public static final String COLUMN_MOVIE_NETID = "netId";
	public static final String COLUMN_MOVIE_TITLE = "title";
	public static final String COLUMN_MOVIE_ORIGINAL_TITLE = "original_title";
	public static final String COLUMN_MOVIE_DESCRIPTION = "description";
	public static final String COLUMN_MOVIE_GENRE = "genre";
	public static final String COLUMN_MOVIE_YOUTUBE = "youtube_link";
	public static final String COLUMN_MOVIE_IMDB = "imdbLink";
	public static final String COLUMN_MOVIE_PICTURE = "picture_link";
	public static final String COLUMN_MOVIE_POSTER = "poster_link";
	public static final String COLUMN_MOVIE_RATING = "imdb_rating";
	public static final String COLUMN_MOVIE_DURATION = "duration";

	public static final String TABLE_SESSIONS = "sessions";
	public static final String COLUMN_SESSION_ID = "_id";
	public static final String COLUMN_SESSION_CINEMAID = "cinema_id";
	public static final String COLUMN_SESSION_MOVIEID = "movie_id";
	public static final String COLUMN_SESSION_DATETIME = "datetime";
	public static final String COLUMN_SESSION_PICTURE = "picture_link";


	private static final String DATABASE_NAME = "kinokavad.db";
	private static final int DATABASE_VERSION = 3;

	private static final String DATABASE_CREATE_CINEMAS = "create table "
			+ TABLE_CINEMAS + "("
			+ COLUMN_CINEMA_ID + " integer primary key autoincrement, " 
			+ COLUMN_CINEMA_NETID + " integer, "
			+ COLUMN_CINEMA_NAME + " text, "
			+ COLUMN_CINEMA_CITY + " text, "
			+ COLUMN_CINEMA_SELECTED + " integer, "
			+ COLUMN_CINEMA_PHONE + " text, "
			+ COLUMN_CINEMA_EMAIL + " text, "
			+ COLUMN_CINEMA_ADDRESS + " text, "
			+ COLUMN_CINEMA_LAT + " real, "
			+ COLUMN_CINEMA_LON + " real, "
			+ COLUMN_CINEMA_INFO + " text, "
			+ COLUMN_CINEMA_HOMEPAGE + " text);";

	private static final String DATABASE_CREATE_MOVIES = "create table "
			+ TABLE_MOVIES + "("
			+ COLUMN_MOVIE_ID + " integer primary key autoincrement, " 
			+ COLUMN_MOVIE_NETID + " integer, "
			+ COLUMN_MOVIE_TITLE + " text, "
			+ COLUMN_MOVIE_ORIGINAL_TITLE + " text, "
			+ COLUMN_MOVIE_DESCRIPTION + " text, "
			+ COLUMN_MOVIE_GENRE + " text, "
			+ COLUMN_MOVIE_YOUTUBE + " text, "
			+ COLUMN_MOVIE_IMDB + " text, "
			+ COLUMN_MOVIE_PICTURE + " text, "
			+ COLUMN_MOVIE_POSTER + " text, "
			+ COLUMN_MOVIE_RATING + " text, "
			+ COLUMN_MOVIE_DURATION + " text);";

	private static final String DATABASE_CREATE_SESSIONS = "create table "
			+ TABLE_SESSIONS + "("
			+ COLUMN_SESSION_ID + " integer primary key autoincrement, " 
			+ COLUMN_SESSION_CINEMAID + " integer, "
			+ COLUMN_SESSION_MOVIEID + " integer, "
			+ COLUMN_SESSION_DATETIME + " text, "
			+ COLUMN_SESSION_PICTURE + " text);";

	public SQLiteHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(DATABASE_CREATE_CINEMAS);
		db.execSQL(DATABASE_CREATE_MOVIES);
		db.execSQL(DATABASE_CREATE_SESSIONS);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.w(SQLiteHelper.class.getName(),
				"Upgrading database from version " + oldVersion + " to "
						+ newVersion + ", which will destroy all old data");

		db.execSQL("DROP TABLE IF EXISTS " + TABLE_CINEMAS);
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_MOVIES);
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_SESSIONS);
		onCreate(db);

	}

}
